//
//  XXView.swift
//  XXSwiftDemo
//
//  Created by admin on 2022/3/28.
//

import UIKit

class XXView: UIView {

    var tapBlock:XXVoidBlock?{
        didSet{
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
            self.isUserInteractionEnabled = true
            self.addGestureRecognizer(tap)
        }
    }
    @objc func tapAction(){
        if let b = tapBlock{
            b()
        }
    }
    init(frame: CGRect, color:UIColor) {
        super.init(frame: frame)
        self.setupUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI() {}
}

public extension UIView{
    
    var x:CGFloat {
        get{
            return frame.origin.x
        }
        set{
            var newFrame = frame
            newFrame.origin.x = newValue
            frame = newFrame
        }
    }
    var y:CGFloat {
        get{
            return frame.origin.y
        }
        set{
            var newFrame = frame
            newFrame.origin.y = newValue
            frame = newFrame
        }
    }
    var width:CGFloat {
        get{
            return frame.size.width
        }
        set{
            var newFrame = frame
            newFrame.size.width = newValue
            frame = newFrame
        }
    }
    var height:CGFloat {
        get{
            return frame.size.height
        }
        set{
            var newFrame = frame
            newFrame.size.height = newValue
            frame = newFrame
        }
    }
    var top:CGFloat {
        get{
            return frame.origin.y
        }
        set{
            var newFrame = frame
            newFrame.origin.y = newValue
            frame = newFrame
        }
    }
    var left:CGFloat {
        get{
            return frame.origin.x
        }
        set{
            var newFrame = frame
            newFrame.origin.x = newValue
            frame = newFrame
        }
    }
    var bottom:CGFloat {
        get{
            return frame.origin.y + frame.size.height
        }
        set{
            var newFrame = frame
            newFrame.origin.y = newValue-frame.size.height
            frame = newFrame
        }
    }
    var right:CGFloat {
        get{
            return frame.origin.x + frame.size.width
        }
        set{
            var newFrame = frame
            newFrame.origin.x = newValue-frame.size.width
            frame = newFrame
        }
    }
    var centerX:CGFloat {
        get{
            return center.x
        }
        set{
            var newCenter = center
            newCenter.x = newValue
            center = newCenter
        }
    }
    var centerY:CGFloat {
        get{
            return center.y
        }
        set{
            
            var newCenter = center
            newCenter.y = newValue
            center = newCenter
        }
    }
    var cornerRadius:CGFloat{
        get{
            return layer.cornerRadius
        }
        set{
            layer.cornerRadius = newValue
            layer.masksToBounds = true
        }
    }
    var borderColor:UIColor{
        get{
            return UIColor.init(cgColor: layer.borderColor ?? UIColor.background.cgColor)
        }
        set{
            layer.borderColor = newValue.cgColor
        }
    }
    
    fileprivate func setupShader(_ w: CGFloat, _ h: CGFloat){
        //设置阴影路径--避免离屏渲染
        let path = UIBezierPath(rect: self.bounds)
        self.layer.shadowPath = path.cgPath
           //设置阴影颜色
        self.layer.shadowColor = UIColor.black.cgColor
           //设置透明度
        self.layer.shadowOpacity = 0.5
           //设置阴影半径
        self.layer.shadowRadius = 5.0
           //设置阴影偏移量
        self.layer.shadowOffset = CGSize(width: w, height: h)
    
    }
    func topOffset(_ offset:CGFloat) -> CGFloat {
        return top+offset
    }
    
    func leftOffset(_ offset:CGFloat) -> CGFloat {
        return left+offset
    }
    
    func rightOffset(_ offset:CGFloat) -> CGFloat {
        return right+offset
    }
    
    func bottomOffset(_ offset:CGFloat) -> CGFloat {
        return bottom+offset
    }
    
    func border(color:UIColor,width:CGFloat) {
        self.borderColor = color
        self.layer.borderWidth = width
    }

    // 获取View所在的控制器，响应链上的第一个Controller
    func viewController() -> UIViewController? {
        var nextResponder = self as UIResponder?
        repeat {
            nextResponder = nextResponder?.next!;
            if nextResponder is UIViewController {
                return nextResponder as? UIViewController
            }
        }while (nextResponder != nil)
        return nil;
    }
    
    // 清空所有子视图
    func removeAllSubviews() {
        for view in self.subviews {
            view.removeFromSuperview()
        }
    }
    
    // 视图快照(截图)
    func snapshotImage() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0)
        self.layer.render(in: (UIGraphicsGetCurrentContext())!)
        let snap = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return snap!
    }
}
